package com.pro.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.pro.dao.impl.BannerDaoImpl;
import com.pro.dao.impl.ProductTypeDaoImpl;
import com.pro.entity.BannerEntity;
import com.pro.entity.ProductTypeEntity;
import com.pro.util.BaseServlet;
import com.pro.util.Result;
import com.pro.util.ResultToLayui;

/**
 * Servlet implementation class TestServlet
 * @param <Gson>
 */
public class ProductTypeAdminServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	ProductTypeDaoImpl pDaoImpl = new ProductTypeDaoImpl();
	
	public void test1(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		
		  ArrayList<ProductTypeEntity> aList = pDaoImpl.productType();
		  PrintWriter out = response.getWriter(); 
		  out.print(ResultToLayui.toClient(0, "", aList.size(), aList));
		  out.flush(); 
		  out.close(); 
	}
	
	public void productTypeUpdate(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		
		  int id= Integer.parseInt(request.getParameter("productTypeId"));
		  String productTypeName = request.getParameter("productTypeName");
		  Date date = new Date();
		  String createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		  int result = pDaoImpl.productTypeUpdate(new ProductTypeEntity(id, createTime, productTypeName));
		  
		  PrintWriter out = response.getWriter(); 
		  out.print(Result.toClient("修改", result>0?true:false));
		  out.flush(); 
		  out.close(); 
	}
	
	public void productTypeAdd(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		
		  String productTypeName = request.getParameter("productTypeName");
		  Date date = new Date();
		  String createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		  int result = pDaoImpl.productTypeAdd(new ProductTypeEntity(0, createTime, productTypeName));
		  
		  PrintWriter out = response.getWriter(); 
		  out.print(Result.toClient("添加", result>0?true:false));
		  out.flush(); 
		  out.close(); 
	}
	
	public void productTypeDelete(HttpServletRequest request,HttpServletResponse response) throws Exception {
		int id= Integer.parseInt(request.getParameter("productTypeId"));
		int result=pDaoImpl.productTypeDelete(id);
		// 我们把数据输出到页面上
		PrintWriter printWriter = response.getWriter();
		printWriter.print(Result.toClient("删除",result>0?true:false));
		printWriter.flush();
		printWriter.close();
        }
}
